﻿
CREATE PROCEDURE [dbo].[ContragentsUpdate]
(
	@Name varchar(100),
	@ShortName varchar(20),
	@IsSeller bit,
	@IsCustomer bit,
	@IsSertification bit,
	@EMail varchar(50),
	@Phone varchar(50),
	@DirectorFIO varchar(100),
	@DirectorPosition varchar(50),
	@Note varchar(250),
	@FirmType varchar(10),
	@JuridicalAddress varchar(250),
	@INN varchar(12),
	@KPP varchar(20),
	@OKPO varchar(20),
	@OKONX varchar(20),
	@SettlementAccount varchar(20),
	@Bank varchar(100),
	@BIK varchar(10),
	@CorrespondentAccount varchar(20),
	@ActualAddress varchar(250),
	@Fax varchar(50),
	@WebPage varchar(50),
	@ICQ varchar(50),
	@ToDelete bit,
	@MD datetime,
	@MW varchar(50),
	@Original_Id int,
	@Original_MD datetime
)
AS
DECLARE @strMsg nvarchar(500), @intRet int, @intErrCode int
DECLARE @orgName varchar(100), @orgShortName varchar(20), @orgToDelete bit
SET NOCOUNT ON

IF (@Name Is Null) OR (@ShortName Is Null) OR (@ToDelete Is Null) 
OR (@IsSeller Is Null) OR (@IsCustomer Is Null) OR (@IsSertification Is Null)
OR (@Original_Id Is Null) OR (@Original_MD Is Null)
BEGIN
	SELECT @intErrCode = 1, @strMsg = 'Неправильные параметры в вызове процедуры ' + Object_Name(@@Procid) + ' !'
	RAISERROR (@strMsg, 11, @intErrCode)
	RETURN @intErrCode
END

IF (ltrim(@Name) = '') OR (ltrim(@ShortName) = '')
BEGIN
	SELECT @intErrCode = 2, @strMsg = 'Незаполнено наименование или короткое наименование!'
	RAISERROR (@strMsg, 11, @intErrCode)
	RETURN @intErrCode
END

SELECT @orgName = Name, @orgShortName = ShortName, @orgToDelete = ToDelete
FROM [CA].[Contragents] WHERE Id = @Original_Id AND MD = @Original_MD
IF (@@rowcount <> 1)
BEGIN
	SELECT @intErrCode = 3, 
	@strMsg = 'Данные изменены после последнего запроса!'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

IF @orgToDelete = 1 --Запись была отмечена для удаления
BEGIN
	IF @ToDelete = 1 
	BEGIN
		SELECT @intErrCode = 4, 
		@strMsg = 'Нельзя редактировать запись, отмеченную для удаления!'
		RAISERROR (@strMsg, 11, 1)
		RETURN @intErrCode
	END
END ELSE BEGIN
	IF @ToDelete = 1 
	BEGIN
		SELECT @intErrCode = 5, 
		@strMsg = 'Для удаления данных должна использоваться другая процедура!'
		RAISERROR (@strMsg, 11, 1)
		RETURN @intErrCode
	END
END

IF @orgName <> @Name
BEGIN
	IF EXISTS (SELECT TOP 1 ID FROM [CA].[Contragents] WHERE Name = @Name AND ToDelete = 0) 
	BEGIN
		SELECT @intErrCode = 6, @strMsg = 'Дублируется наименование!'
		RAISERROR (@strMsg, 11, @intErrCode)
		RETURN @intErrCode
	END
END

IF @orgShortName <> @ShortName
BEGIN
	IF EXISTS (SELECT TOP 1 ID FROM [CA].[Contragents] WHERE ShortName = @ShortName AND ToDelete = 0) 
	BEGIN
		SELECT @intErrCode = 7, @strMsg = 'Дублируется короткое наименование!'
		RAISERROR (@strMsg, 11, @intErrCode)
		RETURN @intErrCode
	END
END
EXEC [dbo].[ProcessStatistic] @Note OUTPUT, @MD OUTPUT, @MW OUTPUT
	SET NOCOUNT OFF;
UPDATE [CA].[Contragents] SET [Name] = @Name, [ShortName] = @ShortName, [IsSeller] = @IsSeller, [IsCustomer] = @IsCustomer, [IsSertification] = @IsSertification, [EMail] = @EMail, [Phone] = @Phone, 
DirectorFIO = @DirectorFIO,
DirectorPosition = @DirectorPosition,
[Note] = @Note, [FirmType] = @FirmType, [JuridicalAddress] = @JuridicalAddress, [INN] = @INN, [KPP] = @KPP, [OKPO] = @OKPO, [OKONX] = @OKONX, [SettlementAccount] = @SettlementAccount, [Bank] = @Bank, [BIK] = @BIK, [CorrespondentAccount] = @CorrespondentAccount, [ActualAddress] = @ActualAddress, [Fax] = @Fax, [WebPage] = @WebPage, [ICQ] = @ICQ, 
[MD] = @MD, [MW] = @MW, ToDelete = @ToDelete
WHERE Id = @Original_Id AND MD = @Original_MD;
	
SELECT Id, GId, Name, ShortName, IsSeller, IsCustomer, IsSertification, EMail, Phone, 
DirectorFIO, DirectorPosition, Note, FirmType, JuridicalAddress, INN, KPP, OKPO, OKONX, SettlementAccount, Bank, BIK, CorrespondentAccount, ActualAddress, Fax, WebPage, ICQ, 
MD, MW, ToDelete
FROM CA.Contragents WHERE (Id = @Original_Id)

RETURN 0
